package com.ccsc.auth.controller;

import com.ccsc.auth.entity.*;
import com.ccsc.auth.entity.vo.MenuVo;
import com.ccsc.auth.global.Constants;
import com.ccsc.auth.utils.BaseResponse;
import com.ccsc.auth.utils.SSOUtil;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;

/**
 * Created by yyangs on 2017/2/21.
 */
@RestController
@RequestMapping("/menu")
public class MenuController extends BaseController {
    //获取目录
    @RequestMapping(value = "/getMenu", method = RequestMethod.GET, produces = {"application/json", "application/xml"})
    public BaseResponse getMenu() throws Exception {
        try {
            List<ShiroResource> shiroResources = Lists.newArrayList();
            List<MenuVo> menuVoList = Lists.newArrayList();
            ShiroUser sessionUser = getSessionUser();

            if(sessionUser != null) {
                List<ShiroRole> roleList = sessionUser.getRoles();
                shiroResources = SSOUtil.getResourcesBySysKey(sessionUser, Constants.SSO_APP_KEY);

                if(shiroResources!=null && shiroResources.size()>0){
                    for(ShiroResource shiroResource : shiroResources){
                            menuVoList.add(convResource2MenuVo(shiroResource));
                    }
                }
            }
            else {
                return this.error(500, "未获取到用户信息");
            }

            if(menuVoList.size()>0) {
                return this.ok(resourceList2ChildList(menuVoList, null));
            }
            else {
                return this.error(Constants.NO_AUTHORITY, "无访问权限");
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return this.error(500);
        }
    }

    /**
     *将平行的资源树转化层级资源树
     * @param resourceList
     * @param parentNum
     * @return
     */
    public static List<MenuVo> resourceList2ChildList(List<MenuVo> resourceList,Integer parentNum){
        List<MenuVo> temp=new LinkedList<MenuVo>();
        if(resourceList.size()==0){return null;}
        for (int i=0; i<resourceList.size(); i++) {
            if(resourceList.size()==0){break;}
            MenuVo menuVo = resourceList.get(i);
            if(parentNum!=null) {
                if(menuVo.getpId()==null || menuVo.getpId() == 0){continue;}
                if (parentNum==menuVo.getpId()) {
                    resourceList.remove(i);
                    i--;
                    menuVo.setChild(resourceList2ChildList(resourceList, menuVo.getId()));
                    temp.add(menuVo);
                }
            }else {
                if (menuVo.getpId()==null || menuVo.getpId() == 0) {
                    resourceList.remove(i);
                    i--;
                    menuVo.setChild(resourceList2ChildList(resourceList, menuVo.getId()));
                    temp.add(menuVo);
                }
            }
        }
        return temp;
    }

    private MenuVo convResource2MenuVo(ShiroResource resource){
            MenuVo menuVo=new MenuVo();
            menuVo.setId(resource.getResourceId());
            menuVo.setName(resource.getResourceDescription());
            menuVo.setUrl(resource.getResourceContent());
            menuVo.setpId(resource.getResourceParentId());
            menuVo.setIcon(resource.getResourceIcon());
            menuVo.setModuleType(resource.getResourceType());
            return menuVo;
    }

    //获取目录
    @RequestMapping(value = "/getSys", method = RequestMethod.GET, produces = {"application/json", "application/xml"})
    public BaseResponse getSys() throws Exception {
        List<Map<String,Object>> list = Lists.newLinkedList();
        try{
            ShiroUser sessionUser = getSessionUser();
            List<ShiroSystem> systemList = sessionUser.getSystems();

            Collections.sort(systemList, new Comparator<ShiroSystem>() {
                @Override
                public int compare(ShiroSystem o1, ShiroSystem o2) {
                    return o1.getSortSeq().compareTo(o2.getSortSeq());
                }
            });

            for(ShiroSystem shiroSystem : systemList){
                if(!Constants.SSO_APP_KEY.equals(shiroSystem.getSystemIdentifier())){
                    ImmutableMap<String, Object> map = ImmutableMap.of("systemId",shiroSystem.getSystemId(),"systemName",shiroSystem.getSystemName(),"systemUrl",shiroSystem.getSystemUrl());
                    list.add(map);
                }
            }
            return this.ok(list);
        }
        catch (Exception ex)
        {
            return this.error(500);
        }
    }

    //获取用户信息
    @RequestMapping(value = "/getUserInfo", method = RequestMethod.GET, produces = {"application/json", "application/xml"})
    public BaseResponse getUserInfo() throws Exception {
        try{
            ShiroUser sessionUser = getSessionUser();
            return this.ok(sessionUser.getUserName());
        }
        catch (Exception ex)
        {
            return this.error(500);
        }
    }
}
